home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Madness 2 #12 / Multimedia Madness - Volume 2 - Issue 12 (SAMS Publishing) (November 25, 1992).iso / nemo / stringpp / demo.cpp next >
C/C++ Source or Header  |  1992-10-08  |  10KB  |  298 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 2.10           demo.cpp       Last revised 10/08/92 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991, 1992 by Carl W. Moreland                             */
  6. /* -------------------------------------------------------------------- */
  7. /* Demonstration of String++ methods.                                   */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include "str.h"
  14.  
  15. void pause(void);
  16.  
  17. void intro()
  18. {
  19.   clrscr();
  20.   _setcursortype(_NOCURSOR);
  21.  
  22.   String title1 = "String++ Version 2.10";
  23.   String title2 = "Written by Carl W. Moreland";
  24.   String title3 = "Demonstration of methods & operators";
  25.   String title4 = "Hit any key to continue,";
  26.   String title5 = "or <Ctrl>C to exit...";
  27.  
  28.   cout << "\n\n\n\n\n\n\n\n";
  29.   cout << justify(title1, CENTER_JUSTIFY, 78) << "\n";
  30.   cout << justify(title2, CENTER_JUSTIFY, 78) << "\n\n";
  31.   cout << justify(title3, CENTER_JUSTIFY, 78) << "\n\n";
  32.   cout << justify(title4, CENTER_JUSTIFY, 78) << "\n";
  33.   cout << justify(title5, CENTER_JUSTIFY, 78) << "\n";
  34.  
  35.   pause();
  36. }
  37.  
  38. void test1()
  39. {
  40.   cout << "Create a string and assign it \"Hello World.\":\n";
  41.   string str1 = "Hello World.";
  42.   cout << "string str1 = \"Hello World.\";\n\n";
  43.  
  44.   cout << "The string contents are returned by the () operator:\n";
  45.   cout << "str1() = " << str1 << "\n\n";
  46.  
  47.   cout << "Now assign str1 to a second string:\n";
  48.   string str2 = str1;
  49.   cout << "string str2 = str1;\n";
  50.   cout << "str2() = " << str2 << "\n\n";
  51.  
  52.   cout << "We can also assign numeric values to strings:\n";
  53.   str2 = 1024;
  54.   cout << "str2 = 1024;\n";
  55.   cout << "str2() = │" << str2 << "│\n\n";
  56.  
  57.   cout << "Create multiple characters by passing an optional multiplier to\n";
  58.   cout << "  the string constructor:\n\n";
  59.   str2 = "/* " + string('-', 40) + " */";
  60.   cout << "str2 = \"/* \" + string(\'-\', 40) + \" */\";\n";
  61.   cout << "str2() = " << str2() << "\n\n";
  62.  
  63.   pause();
  64.  
  65.   cout << "Placing a number in the () operator returns the substring\n";
  66.   cout << "  of the string starting at that number:\n";
  67.   cout << "str1(6) = " << str1(6) << "\n\n";
  68.  
  69.   cout << "Placing a second number in the () operator limits the substring\n";
  70.   cout << "  to that many characters:\n";
  71.   cout << "str1(6,2) = " << str1(6,2) << "\n\n";
  72.  
  73.   cout << "The nth character is returned by the [] operator:\n";
  74.   cout << "str1[6] = " << str1[6] << "\n\n";
  75.  
  76.   cout << "The [] operator can also be used to replace the nth character:\n";
  77.   str1[6] = 'w';
  78.   cout << "str1[6] = 'w';\n";
  79.   cout << "str1() = " << str1 << "\n\n";
  80.  
  81.   cout << "The length of str1 is given by the Length() member function:\n";
  82.   cout << "str1.Length() = " << str1.Length() << "\n\n";
  83.  
  84.   pause();
  85.  
  86.   cout << "The == operator will work for string == string, string == char*,\n";
  87.   cout << "  or for char* == string:\n\n";
  88.   if(str1 == "Hello world.")
  89.     cout << "str1 == \"Hello world.\"\n";
  90.   else
  91.     cout << "str1 != \"Hello world.\"\n";
  92.  
  93.   if("Hello world." == str1)
  94.     cout << "\"Hello world.\" == str1\n";
  95.   else
  96.     cout << "\"Hello world.\" != str1\n";
  97.  
  98.   pause();
  99.  
  100.   cout << "Let's create a string that's equal to str1*2:\n\n";
  101.   str2 = str1*2;
  102.   cout << "str2 = str1*2;\n";
  103.   cout << "str2() = " << str2() << "\n\n";
  104.  
  105.   cout << "Now multiply str2 by 2:\n\n";
  106.   str2 *= 2;
  107.   cout << "str2 *= 2;\n";
  108.   cout << "str2() = " << str2() << "\n\n";
  109.  
  110.   pause();
  111.  
  112.   cout << "The C-style function toupper() will return the upper case version\n";
  113.   cout << "  of a string without changing the string itself, whereas the\n";
  114.   cout << "  member function toUpper() will convert the string internally:\n\n";
  115.   cout << "str1() = " << str1 << "\n";
  116.   cout << "toupper(str1) = " << toupper(str1) << "\n";
  117.   cout << "str1() = " << str1 << "\n";
  118.   str1.toUpper();
  119.   cout << "str1.toUpper();\n";
  120.   cout << "str1() = " << str1 << "\n\n";
  121.  
  122.   pause();
  123. }
  124.  
  125. void test2()
  126. {
  127.   cout << "Create a new string with the contents \"only\":\n\n";
  128.   string str3 = "only";
  129.   cout << "string str3 = \"only\";\n\n";
  130.  
  131.   cout << "Now use the + operator to add to it:\n\n";
  132.   str3 = "This is " + str3 + " a test";
  133.   cout << "str3 = \"This is \" + str3 + \" a test\";\n";
  134.   cout << "str3() = " << str3() << "\n\n";
  135.  
  136.   cout << "The Delete() member function can be used to delete a substring:\n\n";
  137.   str3.Delete(8, 5);
  138.   cout << "str3.Delete(8, 5);\n";
  139.   cout << "str3() = " << str3() << "\n\n";
  140.  
  141.   cout << "The Insert() member function can be used to insert a substring:\n\n";
  142.   str3.Insert(8, "still ");
  143.   cout << "str3.Insert(8, \"still \");\n";
  144.   cout << "str3() = " << str3() << "\n\n";
  145.  
  146.   pause();
  147.  
  148.   cout << "Create a new string and use the += operator to append to it:\n\n";
  149.   string str4 = "Please ";
  150.   str4 += "stand by...";
  151.   cout << "string str4 = \"Please \";\n";
  152.   cout << "str4 += \"stand by...\";\n";
  153.   cout << "str4() = " << str4() << "\n\n";
  154.  
  155.   cout << "Use the left(), mid(), & right() functions to return substrings:\n\n";
  156.   cout << " left(str4, 6)    = " << left(str4, 6)   << "\n";
  157.   cout << "  mid(str4, 7, 5) = " << mid(str4, 7, 5) << "\n";
  158.   cout << "right(str4, 5)    = " << right(str4, 5)  << "\n\n";
  159.  
  160.   pause();
  161.  
  162.   cout << "The justify() function will expand a string to a total width of n\n";
  163.   cout << "  by adding blanks and justify the non-blank portion:\n\n";
  164.   string str5 = "Hello world.";
  165.   string str5a = "│" + justify(str5, LEFT, 20)   + "│";
  166.   string str5b = "│" + justify(str5, CENTER, 20) + "│";
  167.   string str5c = "│" + justify(str5, RIGHT, 20)  + "│";
  168.  
  169.   cout << "string str5 = str1;\n";
  170.   cout << "str5a = \"│\" + justify(str5, LEFT, 20)   + \"│\";\n";
  171.   cout << "str5b = \"│\" + justify(str5, CENTER, 20) + \"│\";\n";
  172.   cout << "str5c = \"│\" + justify(str5, RIGHT, 20)  + \"│\";\n";
  173.   cout << "str5a() = " << str5a() << "\n";
  174.   cout << "str5b() = " << str5b() << "\n";
  175.   cout << "str5c() = " << str5c() << "\n";
  176.  
  177.   cout << "\nHere's what happens when clipping is used:\n\n";
  178.   str5a = "│" + justify(str5, LEFT, 8, CLIP)   + "│";
  179.   str5b = "│" + justify(str5, CENTER, 8, CLIP) + "│";
  180.   str5c = "│" + justify(str5, RIGHT, 8, CLIP)  + "│";
  181.  
  182.   cout << "str5a = \"│\" + justify(str5, LEFT, 8, CLIP)   + \"│\";\n";
  183.   cout << "str5b = \"│\" + justify(str5, CENTER, 8, CLIP) + \"│\";\n";
  184.   cout << "str5c = \"│\" + justify(str5, RIGHT, 8, CLIP)  + \"│\";\n";
  185.   cout << "str5a() = " << str5a() << "\n";
  186.   cout << "str5b() = " << str5b() << "\n";
  187.   cout << "str5c() = " << str5c() << "\n";
  188.  
  189.   pause();
  190.  
  191.   cout << "The trim() function will remove leading and trailing whitespace:\n\n";
  192.   string str6 = "\t\t  " + str5 + " \t ";
  193.   cout << "string str6 = \"\\t\\t  \" + str5 + \" \\t \";\n\n";
  194.  
  195.   string str6a = trim(str6, LEFT);
  196.   cout << "str6a = trim(str6, LEFT);\n";
  197.   cout << "str6a() = │" << str6a() << "│\n\n";
  198.   string str6b = trim(str6, RIGHT);
  199.   cout << "str6b = trim(str6, RIGHT);\n";
  200.   cout << "str6b() = │" << str6b() << "│\n\n";
  201.   string str6c = trim(str6);
  202.   cout << "str6c = trim(str6);\n";
  203.   cout << "str6c() = │" << str6c() << "│\n\n";
  204.  
  205.   cout << "You can also specify the character to be trimmed:\n\n";
  206.   str6 = "Here we go again..........";
  207.   cout << "str6 = " << str6 << "\n";
  208.   str6.Trim(RIGHT, '.');
  209.   cout << "str6.Trim(RIGHT, '.');\n";
  210.   cout << "str6 = " << str6 << "\n";
  211.  
  212.   pause();
  213. }
  214.  
  215. void test3()
  216. {
  217.   int i, pos, num;
  218.   string *array;
  219.   string str1 = "HELLO WORLD.";
  220.   string str3 = "This is only a test";
  221.   string str4 = "Please stand by...";
  222.  
  223.   cout << "The following are AWK functions.\n\n";
  224.  
  225.   cout << "index() returns the location of a substring in a string:\n\n";
  226.   pos = index(str1, "WOR");
  227.   cout << "str1() = " << str1 << "\n";
  228.   cout << "pos = index(str1, \"WOR\";\n";
  229.   cout << "pos = " << pos << "\n";
  230.  
  231.   pause();
  232.  
  233.   cout << "split() will split a string at a given substring delimiter:\n\n";
  234.   string str7 = "d:\\prog\\turboc\\str";
  235.   num = split(str7, &array, "\\");
  236.  
  237.   cout << "string str7 = \"d:\\prog\\turboc\\str\";\n";
  238.   cout << "num = split(str7, array, \"\\\";\n\n";
  239.  
  240.   cout << "In this example, str7 is split using the \\ character as the\n";
  241.   cout << "  delimiter. The results are placed in array, which now contains:\n\n";
  242.   for(i=0; i<num; i++)
  243.     cout << "array[" << i << "] = " << array[i] << "\n";
  244.  
  245.   pause();
  246.  
  247.   cout << "gsub() performs a global substitution. In this example, we want to\n";
  248.   cout << "  replace all \\'s with /'s:\n\n";
  249.   cout << "str7() = " << str7 << "\n";
  250.   i = gsub("\\", "/", str7);
  251.   cout << "i = gsub(\"\\\", \"/\", str7);\n";
  252.   cout << "str7() = " << str7 << "\n";
  253.   cout << "i = " << i << "\n";
  254.  
  255.   pause();
  256.  
  257.   cout << "sub() performs a one-time substitution:\n\n";
  258.   cout << "str1() = " << str1 << "\n";
  259.   i = sub("LO", "P ME,", str1);
  260.   cout << "i = sub(\"LO\", \"P ME,\", str1);\n";
  261.   cout << "str1() = " << str1 << "\n";
  262.   cout << "i = " << i << "\n";
  263.  
  264.   pause();
  265.  
  266.   cout << "substr() returns a substring of the string starting at n. If a\n";
  267.   cout << "  a third parameter is given, it represents the maximum number\n";
  268.   cout << "  of characters to return\n\n";
  269.   string str_3 = substr(str3, 8);
  270.   string str_4 = substr(str4, 7, 5);
  271.   cout << "str3() = " << str3 << "\n";
  272.   cout << "substr(str3, 8) = " << str_3 << "\n\n";
  273.   cout << "str4() = " << str4 << "\n";
  274.   cout << "substr(str4, 7, 5) = " << str_4 << "\n\n";
  275.  
  276.   pause();
  277. }
  278.  
  279. main()
  280. {
  281.   intro();
  282.   test1();
  283.   test2();
  284.   test3();
  285.   return 0;
  286. }
  287.  
  288. void pause(void)
  289. {
  290.   char ch = getch();
  291.   if(!ch)
  292.     getch();
  293.   if(ch == 0x03)
  294.     exit(0);
  295.   clrscr();
  296.   cout << "\n";
  297. }
  298.